#include"worker.h"
int checkUser(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    char sql[256] = {0};
    int si = -1;
    sprintf(sql, "SELECT cryptText,rootId FROM userInformation WHERE name='%s'", userInfo->username);
    puts(sql);
    m_MysqlQuery(pthreadPool->mysql, sql);

    MYSQL_RES *results = mysql_store_result(pthreadPool->mysql);
    if (results->row_count == 0)
    {
        si = -1;
        sendInfo(netfd, &si, sizeof(si));
        mysql_free_result(results);
        return -1;
    }
    else
    {
        si = si_loading;
        sendInfo(netfd, &si, sizeof(si));
        MYSQL_ROW row;
        row = mysql_fetch_row(results);
        strcpy(userInfo->password, row[0]);
        strcpy(userInfo->rootId, row[1]);
        userInfo->pos=atoi(row[1]);
        printf("userInfo password:%s,rootId:%s,pos:%d\n",userInfo->password,userInfo->rootId, userInfo->pos);
        //check password
        char salt[32] = {0};
        takeSalt(salt, userInfo->password);
        //puts(salt);
        sendInfo(netfd, salt, strlen(salt));
        char cbuf[64] = {0};
        recvInfo(netfd, cbuf);
        mysql_free_result(results);
        if (strcmp(cbuf, userInfo->password) == 0)
        {
            return si_success;
        }
        else
        {
            return si_failure;
        }
    }
}

int checkID(int netfd, userInfo_t *userInfo, threadPool_t *pthreadPool)
{
    int ret = recvInfo(netfd, userInfo);
    puts(userInfo->username);
    puts(userInfo->password);
    if (ret > 0)
    {
        ret = checkUser(netfd, userInfo, pthreadPool);
    }
    //printf("ret:%d\n",ret);
    sendInfo(netfd, &ret, sizeof(ret));
    if(ret == si_success){
        sendInfo(netfd, userInfo, sizeof(userInfo_t));
    }
}